import { createRouter, createWebHistory } from 'vue-router'
import IndexView from '../views/IndexView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import NoteView from '@/views/nav/NoteView.vue'
import SquareView from '@/views/nav/SquareView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: IndexView,
      children: [
        {
          path: '',
          redirect: '/note',
        },

        {
          path: '/square',
          name: 'square',
          component: SquareView,
          children: [
            {
              path: '/',
              name: 'index',
              component: () => import('../views/square/IndexView.vue'),
            },
          ],
        },

        {
          path: '/note',
          name: 'note',
          component: NoteView,
          children: [
            {
              path: '',
              redirect: '/note/stock',
            },
            {
              path: 'stock',
              name: 'stock',
              children: [
                {
                  path: '',
                  redirect: '/note/stock/list',
                  name: 'stockList',
                },
                {
                  path: 'list',
                  name: 'list',
                  meta: { title: 'note' },
                  component: () => import('../views/note/stock/IndexView.vue'),
                },
                {
                  path: 'detail',
                  name: 'detail',
                  component: () => import('../views/note/doc/DetailView.vue'),
                },
                {
                  path: 'plan/list',
                  name: 'plan',
                  meta: { title: 'plan' },
                  component: () => import('../views/note/stock/PlanView.vue'),
                },
                {
                  path: 'operate',
                  name: 'operate',
                  component: () => import('../views/note/stock/OperateView.vue'),
                },
              ],
            },
            {
              path: 'doc',
              name: 'doc',
              component: () => import('../views/note/doc/DetailView.vue'),
            },
          ],
        },
      ],
    },

    {
      path: '/login',
      name: 'login',
      component: LoginView,
    },
    {
      path: '/register',
      name: 'register',
      component: RegisterView,
    },
  ],
})

router.beforeResolve((to, from, next) => {
  if (to.name) {
    window.document.title = to.name.toString()
  } else {
    window.document.title = 'Trader'
  }

  next()
})

export default router
